﻿Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO
Imports DevExpress.XtraLayout
Imports DevExpress.XtraEditors

Public Class FormTesKesehatan
    Inherits PageTemplate
    Dim isTambah As Boolean = False
    Dim v_NIS As String = ""
    Dim v_deTANGGAL As String = ""
    Dim v_txtTEKANAN_DARAH As String = ""
    Dim v_txtBB As String = ""
    Dim v_txtOLAH_RAGA As String = ""
    Dim v_txtKONSUMSI_ROKOK As String = ""
    Dim v_SUHU_BADAN As String = ""
    Dim v_txtUMUR As String = ""
    Dim v_cbGOL_DARAH As String = ""
    Dim v_cbMEROKOK As Integer
    Dim v_meCATATAN_GIGI As String = ""
    Dim v_txtDENYUT_NADI As String = ""
    Dim v_txtTINGGI_BADAN As String = ""
    Dim v_txtHOBI As String = ""
    Dim v_txtMULAI_MEROKOK As String = ""

    Private Function cekValidate(ByVal jnsCEK As String) As Boolean
        Dim hasilcek As Boolean = True
        If Trim(txtNIS.Text) = "" Or Trim(deTANGGAL.Text) = "" Then
            hasilcek = False
            MsgBox("NIS atau Tanggal Tes Kesehatan Belum Diisi.", MsgBoxStyle.OkOnly, "Informasi")
        End If
        If jnsCEK = "insert" Or jnsCEK = "update" Then
            If Trim(txtDENYUT_NADI.Text) = "0" Or Trim(txtTEKANAN_DARAH.Text) = "0" Or Trim(txtSUHU_BADAN.Text) = "0" Then
                hasilcek = False
                MsgBox("Pengisian Data Tes Kesehatan Belum Lengkap.", MsgBoxStyle.OkOnly, "Informasi")
            End If
        Else

        End If
        Return hasilcek
    End Function


    Private Sub btnSIMPAN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSIMPAN.Click
        Dim cek As Boolean = False

        Dim eksekusi As String = ""
        Dim eksekusi2 As String = ""
        Dim keterangan As String = ""
        Dim bf As New BinaryFormatter
        Dim ms As New MemoryStream
        Dim bytearr As Byte()
        If meCATATAN_GIGI.Lines.Count > 0 Then
            ms = New MemoryStream
            bf.Serialize(ms, meCATATAN_GIGI.Lines)
            bytearr = ms.ToArray
            Dim vmemo As String = String.Empty
            For i As Integer = 0 To meCATATAN_GIGI.Lines.Count - 1
                vmemo += meCATATAN_GIGI.Lines(i).ToString
                If i < meCATATAN_GIGI.Lines.Count - 1 Then
                    vmemo += "\n"
                End If
            Next
            keterangan = vmemo
            v_meCATATAN_GIGI = replaceInput(vmemo)
        Else
        End If
        If isTambah = True Then
            eksekusi = "insert"
            eksekusi2 = "Menambah"
        Else
            eksekusi = "update"
            eksekusi2 = "Mengubah"
        End If
        If cekValidate(eksekusi) Then
            If eksekusi = "insert" Then

                If Not cani Then
                    MsgBox("Maaf Hak Akses Anda Tidak Terdaftar", MsgBoxStyle.Exclamation)
                    ClearLayout("2")
                    Exit Sub
                End If
            ElseIf eksekusi = "update" Then
                If Not canu Then
                    MsgBox("Maaf Hak Akses Anda Tidak Terdaftar", MsgBoxStyle.Exclamation)
                    ClearLayout("2")
                    Exit Sub
                End If
            End If
            If MsgBox("Anda Yaking Ingin " & eksekusi2 & " Data Tes Kesehatan Dengan" & vbCrLf _
                & "NIS" & vbTab & vbTab & vbTab & ": " & txtNIS.Text & vbCrLf _
                & "Nama" & vbTab & vbTab & vbTab & ": " & txtNAMA.Text & vbCrLf _
                & "Tanggal Tes" & vbTab & vbTab & ": " & deTANGGAL.DateTime.ToString("dd/MM/yyyy") & vbCrLf _
                & "Suhu Badan (Celcius)" & vbTab & ": " & txtSUHU_BADAN.Text & vbCrLf _
                & "Tekanan Darah" & vbTab & vbTab & ": " & txtTEKANAN_DARAH.Text & vbCrLf _
                & "Denyut Nadi (Per Menit)" & vbTab & ": " & txtDENYUT_NADI.Text & vbCrLf _
                , MsgBoxStyle.OkCancel, "Informasi") = MsgBoxResult.Ok Then
                SimpanData(eksekusi)
                ClearLayout("2")
            End If
        Else
            Exit Sub
        End If

        
    End Sub

    Private Sub SimpanData(ByVal jnsSimpan As String)
        Dim strQuery As Object()
        Dim err As String = ""
        inisial()
        Dim dtResulExecute As New DataTable
        Dim ValResult As String = ""

        strQuery = New Object() {jnsSimpan, v_NIS, v_deTANGGAL, v_SUHU_BADAN, v_txtDENYUT_NADI, v_txtTEKANAN_DARAH, _
                                v_txtUMUR, v_txtTINGGI_BADAN, v_txtBB, v_cbGOL_DARAH, v_txtHOBI, v_txtOLAH_RAGA, _
                                v_cbMEROKOK, v_txtMULAI_MEROKOK, v_txtKONSUMSI_ROKOK, v_meCATATAN_GIGI}
        If Utils.executeSP("proc_teskesehatan", strQuery, dtResulExecute) Then
            If Utils.RowExist(dtResulExecute) Then
                Dim MsgResult As String = "Execute Successfull"

                If MsgResult = dtResulExecute(0)(0) Then
                    MsgBox(MsgResult, MsgBoxStyle.Information, "Success")
                    If isTambah = True Or jnsSimpan = "delete" Then
                        'clear_input_box()
                    End If
                Else
                    MsgBox(dtResulExecute(0)(0), MsgBoxStyle.Critical, "Error")
                End If
            End If
        End If
        btnCARI_Click(Nothing, Nothing)

    End Sub

    Private Sub inisial()
        v_NIS = replaceInput(txtNIS.Text)
        v_deTANGGAL = deTANGGAL.DateTime.ToString("yyyy-MM-dd")
        v_txtTEKANAN_DARAH = replaceInput(txtTEKANAN_DARAH.Text)
        v_txtBB = replaceInput(txtBB.Text)
        v_txtOLAH_RAGA = replaceInput(txtOLAH_RAGA.Text)
        v_txtKONSUMSI_ROKOK = replaceInput(txtKONSUMSI_ROKOK.Text)
        v_SUHU_BADAN = replaceInput(txtSUHU_BADAN.Text)
        v_txtUMUR = replaceInput(txtUMUR.Text)
        v_cbGOL_DARAH = replaceInput(cbGOL_DARAH.Text)
        If cbMEROKOK.Checked = True Then
            v_cbMEROKOK = 1
        Else
            v_cbMEROKOK = 0
        End If
        v_meCATATAN_GIGI = replaceInput(meCATATAN_GIGI.Text)
        v_txtDENYUT_NADI = replaceInput(txtDENYUT_NADI.Text)
        v_txtTINGGI_BADAN = replaceInput(txtTINGGI_BADAN.Text)
        v_txtHOBI = replaceInput(txtHOBI.Text)
        v_txtMULAI_MEROKOK = replaceInput(txtMULAI_MEROKOK.Text)
    End Sub

    Private Sub btnHAPUS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHAPUS.Click
        If cekValidate("delete") Then
            If Not cand Then
                MsgBox("Maaf Hak Akses Anda Tidak Terdaftar", MsgBoxStyle.Exclamation)
                ClearLayout("2")
                Exit Sub
            End If
            If MsgBox("Anda Yaking Ingin Menghapus Data Tes Kesehatan Dengan" & vbCrLf _
                & "NIS" & vbTab & vbTab & vbTab & ": " & txtNIS.Text & vbCrLf _
                & "Nama" & vbTab & vbTab & vbTab & ": " & txtNAMA.Text & vbCrLf _
                & "Tanggal Tes" & vbTab & vbTab & ": " & deTANGGAL.DateTime.ToString("dd/MM/yyyy") & vbCrLf _
                & "Suhu Badan (Celcius)" & vbTab & ": " & txtSUHU_BADAN.Text & vbCrLf _
                & "Tekanan Darah" & vbTab & vbTab & ": " & txtTEKANAN_DARAH.Text & vbCrLf _
                & "Denyut Nadi (Per Menit)" & vbTab & ": " & txtDENYUT_NADI.Text & vbCrLf _
                , MsgBoxStyle.OkCancel, "Informasi") = MsgBoxResult.Ok Then
                SimpanData("delete")
                btnCARI_Click(sender, e)
                ClearLayout("2")
            End If
        Else
            Exit Sub
        End If
        
    End Sub

    Private Sub GridControl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridView2.Click
        isTambah = False
        Dim obj As DataRow
        Dim i As Integer
        Dim j As Integer
        j = GridView2.RowCount
        If j > 0 Then
            i = GridView2.FocusedRowHandle
            obj = GridView2.GetDataRow(i)
            btnSIMPAN.Enabled = True
            btnHAPUS.Enabled = True
            deTANGGAL.Text = obj(1).ToString
            txtSUHU_BADAN.Text = obj(2).ToString
            txtDENYUT_NADI.Text = obj(3).ToString
            txtTEKANAN_DARAH.Text = obj(4).ToString
            txtUMUR.Text = obj(5).ToString
            txtTINGGI_BADAN.Text = obj(6).ToString
            txtBB.Text = obj(7).ToString
            cbGOL_DARAH.Text = obj(8).ToString
            txtHOBI.Text = obj(9).ToString
            txtOLAH_RAGA.Text = obj(10).ToString
            If obj(11).ToString = "1" Then
                cbMEROKOK.Checked = True
            Else
                cbMEROKOK.Checked = False
            End If

            txtMULAI_MEROKOK.Text = obj(11).ToString
            txtKONSUMSI_ROKOK.Text = obj(12).ToString
            meCATATAN_GIGI.Text = obj(13).ToString
            setControlAktif(False)
            btnHAPUS.Enabled = True
            btnSIMPAN.Enabled = True
        End If
    End Sub
    Dim cani As Boolean = False
    Dim canu As Boolean = False
    Dim cand As Boolean = False
    Private Sub FormTesKesehatan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LocalSession.SessForm = "TesKesehatan"        
        If LocalSession.NisTesKesehatan = "" Then
            'Exit Sub
        Else
            txtNIS.Text = LocalSession.NisTesKesehatan
            txtNAMA.Text = LocalSession.NamaTesKesehatan
            LocalSession.NisTesKesehatan = ""
            LocalSession.NamaTesKesehatan = ""
            ''setControlAktif(False)
            btnCARI_Click(sender, e)
            'txtUMUR_INPUT.Focus()
        End If
        Dim dr() As DataRow
        'Dim dt As New DataTable
        If Utils.RowExist(LocalSession.dtrole) Then
            dr = LocalSession.dtrole.Select("menuid = '" & LocalSession.PublicMenuId & "'")
            If dr.Length > 0 Then
                '       dt = dr.CopyToDataTable
                cani = IIf(IsDBNull(dr(0)("to_insert")), 0, dr(0)("to_insert"))
                canu = IIf(IsDBNull(dr(0)("to_update")), 0, dr(0)("to_update"))
                cand = IIf(IsDBNull(dr(0)("to_delete")), 0, dr(0)("to_delete"))
            End If
        End If
        'If Not cani And Not canu Then
        '    btnSIMPAN.Enabled = False
        'End If
        
    End Sub

    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCARI.Click
        Dim dlgcari As New DialogCariSiswa
        dlgcari.Text = "Pencarian Data Siswa"
        ClearLayout("semua")
        'setControlAktif(False)
        If dlgcari.ShowDialog() = DialogResult.OK Then
            If Not dlgcari.VarNIS = String.Empty Then
                txtNIS.Text = dlgcari.VarNIS
                txtNAMA.Text = dlgcari.VarNAMA
                btnCARI_Click(sender, e)
                'txtUMUR_INPUT.Focus()
            End If
        End If
    End Sub

    Private Sub ClearLayout(ByVal kolom As String)
        If kolom = "semua" Then
            txtNIS.Text = ""
            txtNAMA.Text = ""
            btnSIMPAN.Enabled = False
            btnHAPUS.Enabled = False

        End If
        
        If kolom = "semua" Or kolom = "1" Then
            For i As Integer = 0 To LayoutControlGroup2.Items.Count - 1
                Dim item As New LayoutControlItem
                If Not LayoutControlGroup2.Items(i).IsGroup Then
                    item = LayoutControlGroup2.Items(i)
                    If TypeOf item.Control Is TextEdit Then
                        item.Control.Text = ""
                    ElseIf TypeOf item.Control Is DateEdit Then
                        item.Control.Text = ""
                    ElseIf TypeOf item.Control Is MemoEdit Then
                        item.Control.Text = ""
                    ElseIf TypeOf item.Control Is CheckEdit Then
                        Dim chkedit As New CheckEdit
                        chkedit = CType(item.Control, CheckEdit)
                        chkedit.Checked = 0
                    End If
                End If
            Next
        End If

        If kolom = "semua" Or kolom = "2" Then
            For i As Integer = 0 To LayoutControlGroup7.Items.Count - 1
                Dim item As New LayoutControlItem
                If Not LayoutControlGroup7.Items(i).IsGroup Then
                    item = LayoutControlGroup7.Items(i)
                    If TypeOf item.Control Is DateEdit Then
                        Dim deedit As New DateEdit
                        deedit = CType(item.Control, DateEdit)
                        deedit.DateTime = Now
                    ElseIf TypeOf item.Control Is TextEdit Then
                        item.Control.Text = "0"
                    ElseIf TypeOf item.Control Is MemoEdit Then
                        item.Control.Text = ""
                    ElseIf TypeOf item.Control Is CheckEdit Then
                        Dim chkedit As New CheckEdit
                        chkedit = CType(item.Control, CheckEdit)
                        chkedit.Checked = 0
                    End If
                End If
            Next
        End If

        If kolom = "semua" Or kolom = "3" Then
            For i As Integer = 0 To LayoutControlGroup5.Items.Count - 1
                Dim item As New LayoutControlItem
                If Not LayoutControlGroup5.Items(i).IsGroup Then
                    item = LayoutControlGroup5.Items(i)
                    If TypeOf item.Control Is TextEdit Then
                        item.Control.Text = ""
                    ElseIf TypeOf item.Control Is DateEdit Then
                        item.Control.Text = ""
                    ElseIf TypeOf item.Control Is MemoEdit Then
                        item.Control.Text = ""
                    ElseIf TypeOf item.Control Is CheckEdit Then
                        Dim chkedit As New CheckEdit
                        chkedit = CType(item.Control, CheckEdit)
                        chkedit.Checked = 0
                    End If
                End If
            Next
        End If

    End Sub

    Private Sub btnCARI_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim dtkesehatansiswa As New DataTable        
        
        GetLoadDataKesehatanSiswa(dtkesehatansiswa)
        If Utils.RowExist(dtkesehatansiswa) Then
            FillLayout(dtkesehatansiswa)
            btnTAMBAH.Enabled = True
            setControlAktif(False)

            Dim dtteskesehatansiswa As New DataTable

            GetLoadDataTesKesehatanSiswa(dtteskesehatansiswa)
            refreshGrid(dtteskesehatansiswa)

        Else
            MsgBox("Data Kesehatan Siswa Tidak Ditemukan.", MsgBoxStyle.OkOnly, "Informasi")
            Return
        End If
    End Sub

    Private Sub GetLoadDataKesehatanSiswa(ByRef dtkesehatansiswa As DataTable)
        Dim strQuery As String()
        Dim err As String = ""
        strQuery = New String() {"*", "v_datakesehatan", "nis", txtNIS.Text}
        If Utils.executeSP("proc_searchallEqual", strQuery, dtkesehatansiswa) Then
            dtkesehatansiswa = dtkesehatansiswa
        End If
    End Sub

    Private Sub GetLoadDataTesKesehatanSiswa(ByRef dtteskesehatansiswa As DataTable)
        Dim strQuery As String()
        Dim err As String = ""
        strQuery = New String() {"*", "teskesehatan", "nis", txtNIS.Text}
        If Utils.executeSP("proc_searchallEqual", strQuery, dtteskesehatansiswa) Then
            dtteskesehatansiswa = dtteskesehatansiswa
        End If
    End Sub

    Private Sub setControlAktif(ByVal aktif As Boolean)
        If aktif Then
            deTANGGAL.Enabled = True
            txtSUHU_BADAN.Properties.ReadOnly = False
            txtTEKANAN_DARAH.Properties.ReadOnly = False
            txtDENYUT_NADI.Properties.ReadOnly = False
            btnHAPUS.Enabled = False
        Else
            deTANGGAL.Enabled = False
            txtSUHU_BADAN.Properties.ReadOnly = False
            txtTEKANAN_DARAH.Properties.ReadOnly = False
            txtDENYUT_NADI.Properties.ReadOnly = False
        End If

    End Sub
    Private Sub FillLayout(ByVal data As DataTable)

        For i As Integer = 0 To LayoutControlGroup5.Items.Count - 1
            Dim item As New LayoutControlItem
            If Not LayoutControlGroup5.Items(i).IsGroup Then
                item = LayoutControlGroup5.Items(i)
                If TypeOf item.Control Is MemoEdit Then
                    Dim ctlmemo As New MemoEdit
                    ctlmemo = CType(item.Control, MemoEdit)
                    Dim rch As New RichTextBox
                    rch.Text = IIf(IsDBNull(data(0)(item.Control.Tag)), "", data(0)(item.Control.Tag))
                    ctlmemo.Lines = rch.Lines
                ElseIf TypeOf item.Control Is TextEdit Then
                    item.Control.Text = IIf(IsDBNull(data(0)(item.Control.Tag)), "", data(0)(item.Control.Tag))
                ElseIf TypeOf item.Control Is CheckEdit Then
                    Dim chkedit As New CheckEdit
                    chkedit = CType(item.Control, CheckEdit)
                    chkedit.Checked = IIf(IsDBNull(data(0)(item.Control.Tag)), 0, data(0)(item.Control.Tag))
                ElseIf TypeOf item.Control Is CheckBox Then
                    Dim chkBox As New CheckBox
                    chkBox = CType(item.Control, CheckBox)
                    chkBox.Checked = IIf(IsDBNull(data(0)(item.Control.Tag)), 0, data(0)(item.Control.Tag))
                End If
            End If

        Next

    End Sub

    Private Sub btnTAMBAH_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTAMBAH.Click
        If Not cani Then
            MsgBox("Maaf Hak Akses Anda Tidak Terdaftar", MsgBoxStyle.Exclamation)
            ClearLayout("2")
            Exit Sub
        End If
        isTambah = True
        setControlAktif(True)
        btnSIMPAN.Enabled = True
        ClearLayout("2")
    End Sub

    Private Sub refreshGrid(ByVal dtteskesehatansiswa As DataTable)
        If Utils.RowExist(dtteskesehatansiswa) Or dtteskesehatansiswa.Rows.Count = 0 Then
            GridControl2.DataSource = dtteskesehatansiswa
            GridControl2.Refresh()
            GridControl2.RefreshDataSource()
            GridView2.RefreshData()
            GridView2.Columns(1).OptionsColumn.FixedWidth = True
        Else
            GridControl2.DataSource = Nothing
            GridControl2.Refresh()
            GridControl2.RefreshDataSource()
            GridView2.RefreshData()
        End If
    End Sub
    Private Function replaceInput(ByVal teks As String) As String
        teks = teks.Replace("\", "\\")
        teks = teks.Replace("'", "\'")
        Return teks
    End Function
End Class
